//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    num: '',
    op: ''
  },
  onLoad: function () {
    
  },
  result: null,
  isClrar: false,
  numBtn: function(e){
    var num = e.target.dataset.val
    if(this.data.num === '0' || this.isClrar){
      this.setData({num: num})
      this.isClrar = false
    }else{
      this.setData({num: this.data.num + num})
    }
  },
  opBtn: function(e){
    var op = this.data.op
    var num = Number(this.data.num)
    this.setData({op: e.target.dataset.val})
    if(this.isClrar){
      return
    }
    this.isClrar = true
    if(this.result ===null){
      this.result = num
      return
    }
    if(op === '+'){
      this.result = this.result + num
    }else if(op === '-'){
      this.result =this.result - num
    } else if (op === '*') {
      this.result = this.result * num
    } else if (op === '/') {
      this.result = this.result / num
    } else if (op === '%') {
      this.result = this.result % num
    }
    this.setData({num: this.result + ''})
  },
  doBtn: function(){
    if(this.isClrar){
      this.setData({num: '0.'})
      this.isClrar = false
      return
    }
    if(this.data.num.indexOf('.') >= 0){
      return
    }
    this.setData({num: this.data.num + '.'})
  },
  delBtn: function(){
    var num = this.data.num.substr(0,this.data.num.length-1)
    this.setData({num: num === '' ? '0' : num})
  },
  resetBtn: function(){
    this.result = null
    this.isClrar = false
    this.setData({num: '0',op: ''})
  }
})
